package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api("菜品相关接口")
public class DishController {

    @Autowired
    public DishService dishService;

    @PostMapping
    @CacheEvict(cacheNames = "DishCache",key = "#dishDTO.categoryId")
    public Result sava(@RequestBody DishDTO dishDTO) {
        dishService.saveDishWithFlavors(dishDTO);
        return Result.success();
    }

    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishDto) {
        PageResult pageResult = dishService.findPage(dishDto);
        return Result.success(pageResult);
    }

    @DeleteMapping
    @CacheEvict(cacheNames = "DishCache",allEntries = true)
    @ApiOperation("批量删除")
    public Result delete(@RequestParam List<Long> ids) {
        dishService.deleteBatch(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<DishVO> getByDishId(@PathVariable Long id) {
        DishVO dishVO = dishService.findById(id);
        return Result.success(dishVO);
    }

    @PutMapping
    @CacheEvict(cacheNames = "DishCache",allEntries = true)
    public Result update(@RequestBody DishDTO dishDTO) {
        dishService.updateByIdWithFlavors(dishDTO);
        return Result.success();
    }
}
